<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>


</head>
<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略">
            </div>
        </div>
    </div>
</div>

<div class="container strategies">
    <h5 class="title"></h5>
    <small><span>0</span>人阅读过此攻略</small>
    <div class="focus">
        <i class="fa fa-heart-o fa-2x" id="collect"><span id="collectCount">0</span><span id="collectName">收藏</span></i>
        <i class="fa fa-thumbs-o-up fa-2x" id="praise"><span id="praiseCount">0</span> <span id="praiseName">点赞</span></i>
    </div>
    <div>
        <button class="btn btn-success">想去</button>
        <button class="btn btn-info">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">
        <div class="container hot">
            <h6>大家都在看</h6>
            <div render-loop="list">
                <a data-href="/travelContent.html?id=" render-key="list.id" render-fun="getUrl">
                    <button class="btn" render-html="list.title"></button>
                </a>
            </div>
        </div>
        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-key="list.name" render-fun="getIndex"></h6>
                    <div render-loop="list.details">
                        <a data-href="/strategyDetails.html?id=" render-key="list.details.id" render-fun="getUrl">
                            <button class="btn" render-html="list.details.title"></button>
                        </a>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div>
                <span>价格便宜</span>
                <span>很热闹</span>
                <span>交通方便</span>
                <span>吃的很多</span>
                <span>风景优美</span>
                <span>美食很多</span>
                <span>很特别</span>
                <span>爱上这座城</span>
                <span>住宿方便</span>
                <span>刚刚</span>
            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a data-href="/userProfiles.html?id=" render-fun="getUrl" render-key="list.user.id">
                            <img class="rounded-circle"
                                 render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.user.nickName"></span></p>
                        <span class="comment-star" render-key="list.starNum" render-fun="getStar">
                        </span>
                        <span class="comment-date" render-html="list.commentTime"></span>
                        <div class="comment-content">
                            <a data-href="/strategyComment.html?id=" render-fun="getUrl" render-key="list.id">
                                <p render-html="list.content"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-loop="list.imgs">
                            <li>
                                <img render-src="list.imgs.self">
                            </li>
                        </ul>

                        <div class="d-flex icon">
                            <a data-href="strategyComment.html?id=" render-key="list.id" render-fun="getHref">
                                <i class="fa  fa-commenting-o" style="margin-right: 10px;"> 评论 </i>
                                <i class="fa fa-thumbs-o-up fa-2x likeBtn" render-attr="data-json=list.json">
                                <span render-html="list.praiseNum">0</span>
                                </i>
                            </a>
                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>

</div>
</div>

<div class="operation">
    <button class="btn" id="tour">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>

<script>

    var param = getParams();
    $.get("/strategyCatalogs/" + param.id, {"pageSize": 0}, function (data) {
        var index = 1;
        //$(".title").html(data.list[0].strategy.title);
        $.each(data.list,function (index,ele) {
            $(".title").html(ele.strategy.title)

        })
        $(".catalogs").renderValues(data, {
            getIndex: function (item, value) {
                $(item).html(index + "/" + value);
                index++;
            },
            getUrl: function (item, value) {
                $(item).attr("href", $(item).data("href") + value);
            }
        });
    });

    $.get("/travels/2/tags", {"strategyId": param.id, "pageSize": 0}, function (data) {
        $(".hot").renderValues(data, {
            getUrl: function (item, value) {
                $(item).attr("href", $(item).data("href") + value);
            }
        })
    });


    var currentPage = 1;
    var pages;
    var store = [];

    var commentId;//当前评论的id

    if (user) {
        $.get("praises/strategies/" + param.id + "/user/" + user.id, {state: 1}, function (data) {
            travelIds = data;
        })
    }

    function query() {
        $.get("/strategies/" + param.id + "/strategyComments", {
            "currentPage": currentPage,
            "pageSize": 3
        }, function (data) {
            var json = {
                list: $.merge(store, data.list)
            };
            $("#comment").renderValues(json, {
                getUrl: function (item, value) {
                    $(item).attr("href", $(item).data("href") + value);
                },
                getStar: function (item, value) {
                    var temp = "";
                    for (var i = 0; i < value; i++) {
                        temp += '<i class="fa fa-star"></i>'
                    }
                    for (var i = 0; i < 5 - value; i++) {
                        temp += '<i class="fa fa-star-o"></i>'
                    }
                    $(item).html(temp);
                }
            });

            //图片弹出点击动作
            $(".comment-img img").click(function () {
                $(document).dialog({
                    titleShow: false,
                    overlayClose: true,
                    content: '<div style="text-align: center;"><img width="100%" src="' + $(this).attr('src') + '"></div>'
                });
            });
            pages = data.pages;

            $(".likeBtn").each(function(index,item){
                var json = $(this).data("json");
                var commentId= json.id;
                var authorId=json.authorId;
                if (user) {
                    var obj={
                        strategyId:param.id,
                        commentId:commentId,
                        authorId:authorId,
                        userId:user.id,
                        state:1
                    };
                    //查看是否已经点赞
                    $.get("praises/strategies", obj, function (data) {
                        if(data){
                            $(item).removeClass("fa fa-thumbs-o-up fa-2x").addClass("fa fa-thumbs-up fa-2x");
                        }
                    });
                    $(item).click(function () {
                        $.get("praises/strategies",obj,function (data) {
                                if(data){
                                    $.ajax({
                                        url: '/praises/strategies?strategyId=' + param.id + '&commentId=' + commentId + '&authorId='+authorId+'&userId='+user.id+'&state='+1,
                                        method: 'delete',
                                        success: function () {
                                            $(item).removeClass("fa fa-thumbs-up fa-2x").addClass("fa fa-thumbs-o-up fa-2x");
                                            var num = $(item).children().html()//当前元素前一个兄弟节点
                                            $(item).children().html(Number(num) - 1)
                                        }
                                    });
                                }else{
                                    $.post("/praises/strategies", obj, function (data) {
                                        $(item).removeClass("fa fa-thumbs-o-up fa-2x").addClass("fa fa-thumbs-up fa-2x");
                                        var num = $(item).children().html();//当前元素前一个兄弟节点
                                        $(item).children().html(1 + Number(num))
                                    });
                                }
                            });
                    })

                }else{
                    $(document).dialog({
                        type: 'notice',
                        infoText: '请登录了在点赞*_*!',
                        autoClose: 1000,
                        position: 'bottom'  // center: 居中; bottom: 底部
                    });
                    return;
                }
            })
        });

        currentPage++;

    }

    query();
    //滚动条事件
    $(window).scroll(function () {
        //页面滑动查询
        if (($(window).height() + $(document).scrollTop()) >= $(document).height() - 2) {
            if (currentPage <= pages) {
                query();
            } else {
                $(document).dialog({
                    type: 'notice',
                    infoText: '已经到底了',
                    autoClose: 1500,
                    position: 'bottom'  // center: 居中; bottom: 底部
                });
            }
        }
    });

    $("#commentBtn").click(function () {
        window.location.href = "/mine/addComment.html?id=" + param.id;
    });





    //加入旅行单的功能
    //加入旅行单
    $('#tour').click(function () {
        if (user == null) {
            $(document).dialog({
                type: 'confirm',
                content: '你还没有登录,是否现在登录',
                onClickConfirmBtn: function () {
                    window.location.href = '/login.html';
                },
                onClickCancelBtn: function () {

                }
            });
        } else {
            $.get('/users/' + user.id + '/travelLists/'+param.id, function (data) {
                if (data) {
                    $(document).dialog({
                        overlayClose: true,
                        content: '您已经添加,不能重复添加',
                    });
                } else {
                    $(document).dialog({
                        type: 'confirm',
                        content: '是否加入旅行单',
                        onClickConfirmBtn: function () {
                            $.ajax({
                                url: '/users/' + user.id + '/travelLists',
                                type: 'POST',
                                data: {
                                    strategyId: param.id
                                },
                                success:function (data) {
                                    $(document).dialog({
                                        content: '加入成功,请前往我的旅行单查看',
                                    });

                                }
                            });
                        },
                        onClickCancelBtn: function () {

                        }
                    });
                }
            })

        }
    })









    //获取当前是否是点赞状态
    var user = JSON.parse(sessionStorage.getItem("user"));
    if (user) {
        $.get("/praises/" + user.id + "/" + param.id + "/1", function (data) {
            if (data) {
                praiseState = 1;//如果状态为1,则显示为实心
                $("#praise").attr("class", "fa fa-thumbs-up fa-2x");
                $("#praiseName").html("已点赞");
            }
        });
        $.get("/praises/" + user.id + "/" + param.id + "/2", function (data) {
            if (data) {
                $("#collect").attr("class","fa fa-heart fa-2x");
                $("#collectName").html("已收藏");
            }
        })

    }

    //查询点赞的数量
    function queryPraiseNums() {
        $.get("/praises/"+param.id+"/1",function (data) {
            $("#praiseCount").html(data.strategies);
        });
        $.get("/praises/"+param.id+"/2",function (data) {
            $("#collectCount").html(data.strategies);
        });
    }
    //默认查询一次
     queryPraiseNums();


    $("#praise").click(function () {
        //如果当前用户没有登陆,则提示用户登录
        if (user == null) {
            $(document).dialog({
                type: 'notice',
                infoText: '请登录了在点赞*_*!',
                autoClose: 1000,
                position: 'bottom'  // center: 居中; bottom: 底部
            });
            return;
        }

        $.get("/praises/" + user.id + "/" + param.id + "/1", function (data) {
            if (data) {
                $.ajax({
                    url: '/praises/' + param.id + '/' + user.id + '/1',
                    method: 'delete',
                    success: function () {
                        $("#praise").attr("class", "fa fa-thumbs-o-up fa-2x");
                        $("#praiseName").html("点赞");
                        queryPraiseNums();
                    }
                });
            } else {
                $.post("/praises/" + param.id + "/" + user.id, {state: 1}, function (data) {
                    $("#praise").attr("class", "fa fa-thumbs-up fa-2x");
                    $("#praiseName").html("已点赞");
                    queryPraiseNums();
                });
            }
        });
    });
    $("#collect").click(function () {
        //如果当前用户没有登陆,则提示用户登录
        if (user == null) {
            $(document).dialog({
                type: 'notice',
                infoText: '请登录了在点赞*_*!',
                autoClose: 1000,
                position: 'bottom'  // center: 居中; bottom: 底部
            });
            return;
        }
        $.get("/praises/" + user.id + "/" + param.id + "/2", function (data) {
            if (data) {
                $.ajax({
                    url: '/praises/' + param.id + '/' + user.id + '/2',
                    method: 'delete',
                    success: function () {
                        $("#collect").attr("class", "fa fa-heart-o fa-2x");
                        $("#collectName").html("收藏");
                        queryPraiseNums();
                    }
                });
            } else {
                $.post("/praises/" + param.id + "/" + user.id, {state: 2}, function (data) {
                    $("#collect").attr("class", "fa fa-heart fa-2x");
                    $("#collectName").html("已收藏");
                    queryPraiseNums();
                });
            }
        })
    });

    function star() {
        var temp = "";
        for (var i = 0; i < value; i++) {
            temp += '<i class="fa fa-star"></i>'
        }
        for (var i = 0; i < 5 - value; i++) {
            temp += '<i class="fa fa-star-o"></i>'
        }
        $(item).html(temp);
    }
</script>
</body>
</html>